package com.led.datas;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.led.datatype.ExampleDatas;
import com.led.db.DBMgr;

public class ExampleDatasAccess {

	//query all include other who can be seemed
	static final String SQL_QUERY_EXAMPLEDATAS_ALL = 
			"select id, info, datas from  exampleDatas limit 100";
	static final String SQL_QUERY_EXAMPLEDATAS_BYID = 
			"select id, info, datas from  exampleDatas where id = ? limit 100";
	
	public static ResultSet getAllExampleDatas(){
		Connection con = DBMgr.getConnect();
		if(null == con){
			System.out.println("when getAllExampleDatasResultSet, con is null!");
			return null;
		}
		ResultSet rs =null;
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement(SQL_QUERY_EXAMPLEDATAS_ALL);
			rs = ps.executeQuery();
			return rs;
		} catch (SQLException e) {
			e.printStackTrace();
			DBMgr.reset();
		} finally {
//			if(null != ps){
//				try {ps.close();} catch (SQLException e) {e.printStackTrace();}
//			}
		}
		return null;
	}
	public static ExampleDatas getAllExampleDatasById(int id){
		System.out.println("enter ExampleDatasAccess::getAllExampleDatasById(id:" + id + ")");
		Connection con = DBMgr.getConnect();
		if(null == con){
			System.out.println("when getAllExampleDatasResultSet, con is null!");
			return null;
		}
		ResultSet rs =null;
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement(SQL_QUERY_EXAMPLEDATAS_BYID);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if(null == rs || rs.isAfterLast() || !rs.next()){
				System.out.println("not ExampleDatasAccess for id:" + id);
				return null;
			}
			ExampleDatas exampleDatas =  new ExampleDatas(rs);
			return exampleDatas;
		} catch (SQLException e) {
			e.printStackTrace();
			DBMgr.reset();
		} finally {
//			if(null != ps){
//				try {ps.close();} catch (SQLException e) {e.printStackTrace();}
//			}
		}
		return null;
	}
}
